package top.lbqaq.servlet;

import top.lbqaq.pojo.Product;
import top.lbqaq.pojo.ProductIn;
import top.lbqaq.service.ProductInService;
import top.lbqaq.service.ProductService;
import top.lbqaq.util.pageBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.text.spi.DateFormatProvider;
import java.util.Date;

/**
 * @author Gor
 */
public class ProductInServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String up = req.getParameter("up");
        String number = req.getParameter("number");
        String id = req.getParameter("id");
        req.setAttribute("id",id);
        String type = req.getParameter("type");
        String p= req.getParameter("pageNum");
        String update = req.getParameter("update");
        String insert = req.getParameter("name1");
        String insert1 =req.getParameter("insert");
        int pageNum;
        if(p==null)
        { pageNum=1;}
        else {
            pageNum= Integer.parseInt(p);
        }
        ProductInService productInService = new ProductInService();
        if("刷新".equals(up))
        { int pagesize = 5;
            pageBean<ProductIn> productList = productInService.selectAllProductIn(pageNum, pagesize);
            req.setAttribute("productList", productList);
            req.setAttribute("method1", "selectAll");
            req.getRequestDispatcher("/jsp/productIns.jsp").forward(req, resp);
        }

        if("确认".equals(update))
        {int id1= Integer.parseInt(id);
            int num= Integer.parseInt(number);
            int n=productInService.updateInById(num,id1);
            if(n==0)
            {req.setAttribute("error", "数据更新失败");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "更新成功");
                req.getRequestDispatcher("/jsp/productIns.jsp").forward(
                        req, resp);
            }}
        if("新入库".equals(insert1))
        {  req.getRequestDispatcher("/jsp/productIn.jsp").forward(
                req, resp);}
        if("xiu1".equals(type))
        { //ProductInService productInService = new ProductInService();
            // int id = Integer.parseInt(req.getParameter("id"));
            ProductIn product =new ProductIn();
            // product.setId(id)
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date= null;
            try {
                date = sdf.parse (req.getParameter("date"));
            } catch (ParseException e) {
                e.printStackTrace();
            }

            product.setProductInDate(date);
            product.setProductRemark(req.getParameter("remark"));
            product.setProductSupplier(req.getParameter("supplier"));
            product.setProductUnit(req.getParameter("unit"));
            product.setProductName(req.getParameter("name"));
            product.setProductCategory1(req.getParameter("Category1"));
            product.setProductCategory2(req.getParameter("Category2"));
            product.setProductNumber(Integer.parseInt(req.getParameter("number")));
            req.setAttribute("product", product);
            req.getRequestDispatcher("/jsp/productIns.jsp").forward(req, resp);
            }
        if("insert".equals(insert))
        {ProductIn product =new ProductIn();
            // product.setId(id)
            System.out.println(req.getParameter("remark"));
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//            Date date= null;
//            try {
//                date = sdf.parse (req.getParameter("date"));
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
            Date date = new Date();
            SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String now = dateFormat.format(date);
            product.setProductInDate(date);
            product.setProductRemark(req.getParameter("remark"));
            product.setProductSupplier(req.getParameter("supplier"));
            product.setProductUnit(req.getParameter("unit"));
            product.setProductName(req.getParameter("name"));
            product.setProductCategory1(req.getParameter("Category1"));
            product.setProductCategory2(req.getParameter("Category2"));
            product.setProductNumber(Integer.parseInt(req.getParameter("number")));
            int n=productInService.insertproductIn(product);
            if(n==0)
            {req.setAttribute("error", "插入失败");
                req.getRequestDispatcher("/error.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "插入成功");
                int pagesize = 5;
                pageBean<ProductIn> productList = productInService.selectAllProductIn(pageNum, pagesize);
                req.setAttribute("productList", productList);
                req.setAttribute("method1", "selectAll");
                req.getRequestDispatcher("/jsp/productIns.jsp").forward(
                        req, resp);
            }
        }
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }}
